// pages/play/play.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animation: 'play',
    albumImg: '',
    songName: '',
    id: 1867936253,
    isPlay: false, 
    playImg: '../../img/pause.png',
    loopImg: '../../img/loop.png',
    shouimg:'../../image/收藏11.png',
    idList: [], 
    isloop: 1, 
    alltime: '00:00', 
    duration: '',
    nowtime: 0, 
    time: '00:00', 
    prevIdList: [], 
    resultArr: [],
    currentIndex: 0,
    top: 0 
  },

  shouc(){
    var shouc_index=0
   shouc_index++
   if(shouc_index==0){
     this.setData({
     shouimg:'../../image/收藏11.png'
     })   
   }else if(shouc_index==1){
     shouc_index++
    this.setData({
      shouimg:'../../image/收藏11-ac.png',
      shouc_index:0
      })  
      wx.showToast({
        title: '收藏成功',
        icon:'success'
      })
   }
    },
  playMusic() {
    if (this.data.isPlay) {
      this.music.pause()
      this.setData({
        isPlay: false,
        playImg: '../../img/play1.png',
        animation: 'paused'
      })
    } else {
      this.music.play()
      this.setData({
        isPlay: true,
        playImg: '../../img/pause.png',
        animation: 'play'
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
    var list = options.idList
    var list1 = list.split(',')

    wx.request({
      url: 'https://music.163.com/api/song/detail/?id=' + options.id + '&ids=[ '+ options.id + ']',
      success:(res)=>{
        var imgUrl = res.data.songs[0].album.picUrl
        var name = res.data.songs[0].name
        var duration = res.data.songs[0].duration
        duration = Math.floor(duration/1000)
        var minite = Math.floor(duration / 60)
        var second = Math.floor(duration % 60)
        if(minite < 10){
          minite = '0' + minite
        }
        if(second < 10){
          second = '0' + second
        }
        var alltime = minite + ':' + second

        this.setData({
          albumImg: imgUrl,
          songName: name,
          alltime: alltime,
          duration: duration
        })
      }
    })
    this.setData({
      id: options.id,
      idList: list1
    })

    this.requestLyric(options.id)
  },

  nextMusic() {
    if (this.data.isloop == 0) {
      this.music.seek(0) 
      this.setData({
        isPlay: true,
        playImg: '../../img/pause.png',
        animation: 'play'
      })
      this.music.play()
    } else if(this.data.isloop == 1) {
      var idlist = this.data.idList
      var id = this.data.id 
      var num = idlist.indexOf(id) 
      if (num >= idlist.length - 1) {
        var nextId = idlist[0]
      } else {
        var nextId = idlist[num + 1]
      }
      this.request(nextId)
    } else {
      var prevIdList = this.data.prevIdList
      var idlist = this.data.idList 
      var id = this.data.id 
      var num = idlist.indexOf(id) 
      prevIdList.push(idlist[num])
      var num1 = Math.floor(Math.random()*this.data.idList.length)
      if(num == num1){
        if (num >= idlist.length - 1) {
          var nextId = idlist[0]
        } else {
          var nextId = idlist[num1 + 1]
        }
      }else{
        var nextId = idlist[num1]
      }
      this.request(nextId)
    }
  },

  prevMusic() {
    if (this.data.isloop == 0) {
      this.music.seek(0)
      this.setData({
        isPlay: true,
        playImg: '../../img/pause.png',
        animation: 'play'
      })
      this.music.play()
    } else if(this.data.isloop == 1) {
      var idlist = this.data.idList
      var id = this.data.id
      var num = idlist.indexOf(id)
      if (num == 0) {
        var prevId = idlist[idlist.length - 1]
      } else {
        var prevId = idlist[num - 1]
      }
      this.request(prevId)
    } else{
      var idlist = this.data.idList
      var prevIdList = this.data.prevIdList
      var id = this.data.id
      var num = prevIdList.indexOf(id)
      if(num < 0){
        var prevId = prevIdList[prevIdList.length - 1]
      }else if(num == 0){
        var num1 = Math.floor(Math.random()*this.data.idList.length)
        if(num == num1){
          if (num >= idlist.length - 1) {
            var prevId = idlist[0]
          } else {
            var prevId = idlist[num1 + 1]
          }
        }else{
          var prevId = idlist[num1]
        }
      }else{
        var prevId = prevIdList[num - 1]
      }
      this.request(prevId)
    }
  },

  request: function(musicId){
    wx.request({
      url: 'https://music.163.com/api/song/detail/?id=' + musicId + '&ids=[ '+ musicId + ']',
      success: (res)=>{
        // 取到的专辑图片路径
        var imgUrl = res.data.songs[0].album.picUrl
        var name = res.data.songs[0].name
        this.setData({
          albumImg: imgUrl,
          songName: name,
          id: musicId, 
          isPlay: true,
          playImg: '../../img/pause.png',
          animation: 'play'
        })
        /* 播放音乐 */
        this.music.play()
      }
    })
  },

  // 请求歌词
  requestLyric: function(musicId){
    wx.request({
      url: 'https://music.163.com/api/song/lyric?os=pc&id=' + musicId + '&lv=-1&kv=-1&tv=-1',
      success:(res)=>{
        var lyric = res.data.lrc.lyric
        var lyricArr = lyric.split('\n')
        var part = /\[\d{2}:\d{2}\.\d{2,3}\]/
        var resultArr = [] 
        if(lyricArr[lyricArr.length - 1] == ''){
          lyricArr.pop()
        }
        lyricArr.forEach(function(item,index,arr){
          var real_lyric = item.replace(part,'')
          var time = item.match(part)
          time = time[0] 
          time = time.slice(1,-1) 
          var timeArr = time.split(':') 
          var finalTime = timeArr[0]*60 + timeArr[1]*1
          resultArr.push([finalTime, real_lyric])
        })
        this.setData({
          resultArr: resultArr
        })
      }
    })
  },


  loopPlay() {
    var loop = this.data.isloop
    loop ++
    loop = loop % 3
    if (loop == 0) {
      this.setData({
        isloop: loop,
        loopImg: '../../img/single.png'
      })
    } else if(loop == 1) {
      this.setData({
        isloop: loop,
        loopImg: '../../img/loop.png'
      })
    } else if(loop == 2) {
      this.setData({
        isloop: loop,
        loopImg: '../../img/random.png'
      })
    }
  },
  changeTime(event){
    var nowtime = event.detail.currentTime
    var resultArr = this.data.resultArr
    var currentIndex = this.data.currentIndex
    for(var i = 0; i < resultArr.length; i++){
      if(nowtime > resultArr[i][0] && nowtime < resultArr[i + 1][0]){
        this.setData({
          currentIndex: i,
          top: i > 7 ? (i - 5) * 30 : 0
        })
      }
    }
    // 秒数取整
    nowtime = Math.floor(nowtime)
    var fen = Math.floor(nowtime / 60)
    var miao = Math.floor(nowtime % 60)
    if(fen < 10){
      fen = '0' + fen
    }
    if(miao < 10){
      miao = '0' + miao
    }
    var time = fen + ":" + miao
    this.setData({
      time: time,
      nowtime: nowtime
    })
  },
  changeSlider(event){
    var value = event.detail.value
    this.music.seek(value)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.music = wx.createAudioContext('music')
    this.music.play()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})